package com.example.demo.controller;

import com.example.demo.bean.SystemConfig;
import com.example.demo.domain.AjaxResult;
import com.example.demo.utils.InterfaceUtil;
import com.ideabank.po.SOFInterface;
import com.ideabank.po.mof.MOFClient;
import com.ideabank.po.mof.bean.CertIdParams;
import com.ideabank.po.mof.bean.CertParams;
import com.ideabank.po.mof.bean.MOFDecryptResult;
import com.ideabank.po.mof.exception.NewCSSException;
import com.ideabank.util.Base64Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/testMOF")
public class TestMOFController {

    /**
     * 消息签名
     * @param inData
     * @return
     * @throws Exception
     */
    @RequestMapping("/signMessage")
    @ResponseBody
    public AjaxResult signMessage(String inData) throws UnsupportedEncodingException {
        byte[] inDataByte=inData.getBytes("GBK");
        MOFClient mof= null;
        try {
            mof = new MOFClient(SystemConfig.getIp()+":"+SystemConfig.getPort());
        } catch (NewCSSException e) {
            return AjaxResult.ERROR("初始化失败");
        }
        byte[] signdata = null;
        String signdata_base64=null;
        try {
            signdata = mof.sign(SystemConfig.getContainerName(),inDataByte).getSignData();
            signdata_base64= Base64Util.getBASE64(signdata);
            return AjaxResult.SUCCESS(signdata_base64);
        } catch (NewCSSException e) {
            e.printStackTrace();
            return AjaxResult.ERROR(e.getDescription());
        }
    }

    /**
     * 消息验签
     * @param signValue
     * @return
     * @throws Exception
     */

    @RequestMapping("/verifySignMessage")
    @ResponseBody
    public AjaxResult verifySignMessage( String signValue,String inData) throws UnsupportedEncodingException {
        MOFClient mof= InterfaceUtil.getMOFInterface();
        if(mof==null){
            return AjaxResult.ERROR("初始化失败");
        }
        byte[] inDataByte=inData.getBytes("GBK");
        try {
            mof.verifySign(Base64Util.getFromBASE64ToByte(signValue),inDataByte);
            return AjaxResult.SUCCESS();
        } catch (NewCSSException e) {
            e.printStackTrace();
            System.out.println(e.getDescription());
           return AjaxResult.ERROR("验签失败");
        }
    }

    /**
     * 加密数据
     * @param inData
     * @param cert
     * @return
     * @throws Exception
     */
    @RequestMapping("/encryptData")
    @ResponseBody
    public AjaxResult encryptData(String inData,String cert) throws UnsupportedEncodingException {
        MOFClient mof= InterfaceUtil.getMOFInterface();
        if(mof==null){
            return AjaxResult.ERROR("初始化失败");
        }
        byte[] inDataByte=inData.getBytes("GBK");
        try {
            byte[] envelopData = mof.encryptEnvelop(new CertParams[]{new CertIdParams(SystemConfig.getContainerName())}, inData.getBytes()).getEnvelopData();
            return AjaxResult.SUCCESS(Base64Util.getBASE64(envelopData));
        } catch (NewCSSException e) {
            e.printStackTrace();
           return AjaxResult.ERROR(e.getDescription());
        }

    }

    /**
     * 解密数据
     * @param inData
     * @return
     * @throws Exception
     */
    @RequestMapping("/decryptData")
    @ResponseBody
    public AjaxResult decryptData(String inData,String envelopData){
        MOFClient mof= InterfaceUtil.getMOFInterface();
        if(mof==null){
            return AjaxResult.ERROR("初始化失败");
        }
        MOFDecryptResult result= null;
        try {
            result = mof.decryptEnvelop(Base64Util.getFromBASE64ToByte(envelopData));
            if(new String(result.getPlainData()).equals(inData)){
                return AjaxResult.ERROR("解密失败,解密数据和原文不匹配。");
            };
            return AjaxResult.SUCCESS();

        } catch (NewCSSException e) {
            e.printStackTrace();
            return AjaxResult.ERROR("解密失败,"+e.getDescription());
        }
    }


}
